package com.example.myapplication;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static android.content.ContentValues.TAG;

public class MyTask implements Runnable{
    Handler handler;
    public void setHandler(Handler handler) {this.handler=handler;}
    @Override
    public void run(){

        List<String> ret=new ArrayList<>();
        try {
            Thread.sleep(3000);
            Document doc = Jsoup.connect("http://www.boc,cn/sourcedb/whpi").get();
            Log.i(TAG, "title:" + doc.title());
            Element table = doc.getElementsByTag("table").first();
            Elements trs = table.getElementsByTag("tr");
            for (Element tr : trs) {
                Elements tds = tr.getElementsByTag("td");
                if (tds.size() > 0) {
                    String str = tds.get(0).text();
                    String val = tds.get(5).text();
                    Log.i(TAG, "run:tdl=" + "货币名称:" + str + "汇率:" + val);
                    ret.add(str +"=>"+val);
                }
            }
        }
        catch(IOException e){
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Message msg = handler.obtainMessage(9,ret);
        msg.obj="Hello from run()";
        handler.sendMessage(msg);
    }
}